package nuaa.controller;

import nuaa.dao.IIndexDao;
import nuaa.dao.IRecommendDao;
import nuaa.domain.*;
import nuaa.service.DestinationService;
import nuaa.service.IndexService;
import nuaa.service.UserService;
import nuaa.util.UtilSqlSession;
import nuaa.util.UtilString;
import org.apache.ibatis.session.SqlSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 首页控制器
 */
@Controller
public class IndexController {

    /*实例化IndexService对象*/
    private IndexService indexService=new IndexService();
    private DestinationService destinationService=new DestinationService();
    private UserService userService=new UserService();

    public IndexController() throws IOException {
    }

    @RequestMapping("/register")
    public String register(){
        /*
        具体的对注册数据的处理和存储到数据库的操作，在user/register下面
         */
        //跳转到注册成功
        return "register";
    }

    /**
     * 跳转到的登录页面，进行一定程度的操作，再跳转到真正的登录页面
     * @return
     */
    @RequestMapping("/login")
    public String login(){
        return "login";
    }

    /**
     * 推荐页面
     * @return
     */
    @RequestMapping("/recommend")
    public String recommend(Model model) throws IOException {
        //打开数据库，获取数据
        //1.获取sqlSession
        SqlSession sqlSession = UtilSqlSession.getSqlSession();
        IRecommendDao dao = sqlSession.getMapper(IRecommendDao.class);
        //2.获取查询结果
        List<Recommend> recommends = dao.findAll();
        Integer userId;
        Integer destId;
        String username;
        String destinations;
        for (Recommend recommend : recommends) {
            userId = recommend.getUserId();
            destId = recommend.getDestId();
            //根据userId获取人名
            username = userService.findUserNameByUserId(userId);
            //根据destId获取路径
            destinations = destinationService.findDestinationsByDestId(destId);
            //设置到recommend中
            recommend.setUsername(username);
            recommend.setDestinations(destinations);
        }
        //3.放到model中
        model.addAttribute("recommends",recommends);
        return "recommend";
    }

    /**
     * 进入个人中心页面
     * @param pageNum
     * @return
     */
    @RequestMapping("/personalInformation")
    public void personalInformation(String pageNum,String item,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        /*
        1.将新的pageNum放到request域中，使得jsp页面可以取出
        2.将item放到request域中
         */
        //1.判断是否为空，如果为空，设置为0
        boolean empty = UtilString.isEmpty(pageNum);
        if (empty){
            pageNum="0";
        }
        //2.将pageNum放到request域中
        request.setAttribute("pageNum",pageNum);
        //3.判别item是否为空
        if (UtilString.isEmpty(item)){
            item="0";
        }
        //4.将item放到request域中
        request.setAttribute("item",item);
        //5.调用service层参数
        String nextPage=indexService.personalInformation(pageNum,item);
        /*请求转发*/
        request.getRequestDispatcher(nextPage).forward(request,response);
    }

    /**
     * 进入管理员管理页面
     * @param pageNum
     * @return
     */
    @RequestMapping("/adminInfo")
    public void adminInfo(String pageNum,String item,HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        /*
        1.将新的pageNum放到request域中，使得jsp页面可以取出
        2.将item放到request域中
         */
        //1.判断是否为空，如果为空，设置为0
        boolean empty = UtilString.isEmpty(pageNum);
        if (empty){
            pageNum="0";
        }
        //2.将pageNum放到request域中
        request.setAttribute("pageNum",pageNum);
        //3.判别item是否为空
        if (UtilString.isEmpty(item)){
            item="0";
        }
        //4.将item放到request域中
        request.setAttribute("item",item);
        //5.调用service层参数
        String nextPage=indexService.adminInfo(pageNum,item);
        /*请求转发*/
        request.getRequestDispatcher(nextPage).forward(request,response);
    }

    /**
     * 退出登录
     */
    @RequestMapping("/logout")
    public String logout(HttpServletRequest request){
        //1.session中剔除对象
        request.getSession().removeAttribute("user");
        //2.回到首页。使用重定向，可以到达WEN-INF外围
        return "redirect:/index.jsp";
    }

    /**
     * 管理员退出登录
     */
    @RequestMapping("/logoutAdmin")
    public String logoutAdmin(HttpServletRequest request){
        //1.session中剔除对象
        request.getSession().removeAttribute("admin");
        //2.回到首页。使用重定向，可以到达WEN-INF外围
        return "redirect:/index.jsp";
    }

    /**
     * 进入静态页面：队伍对每个人而言，都是一样的
     * @return
     */
    @RequestMapping("/group")
    public String group(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //由视图解析器进行处理，到达pages/group.jsp
        //1.由IndexService得到查询队伍信息
        List<Group> groups = indexService.getGroups();
        //2.放到request作用狱中
        request.setAttribute("groups",groups);
        //3.跳转到这个页面
        return "group";
    }

    /**
     *
     * @param request
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/search")
    public String search(HttpServletRequest request,HttpServletResponse response) throws IOException {
        //1.交给service来处理，获取到。获取到最后的展示结果，放到message中
        List<Group> groups = indexService.search(request, response);
        //2.将查找到的数据放到model中，前端可以取出
        request.setAttribute("searchedGroups",groups);
        return "searchResult";
    }

    /**
     * 测试post
     */
    @RequestMapping("/postTest")
    public @ResponseBody
    String postTest(String name, String pass){
        String str="postTest";
        System.out.println(name+" "+pass);
        System.out.println("进入postTest方法");
        return str;
    }
}
